<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="Hexo Theme Redefine">
    
    <meta name="author" content="The局外人">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://dragon-xjy.atomgit.net/personal/2024/05/14/关于es6这篇就够了/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="ES6（ECMAScript 2015）是JavaScript的一个重要更新版本。它引入了许多新的语言特性和改进，使JavaScript更加现代化和强大。以下是一些ES6的特性以及开发中的使用方法，话不多说开始ES6之旅吧~~~ let变量与const变量块级作用域在日常开发中多存在于使用if&#x2F;for关键字结合let&#x2F;const创建的块级作用域，值得注意的是使用let&amp;#x2F">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6这篇就够了！！">
<meta property="og:url" content="https://dragon-xjy.atomgit.net/personal/2024/05/14/%E5%85%B3%E4%BA%8EEs6%E8%BF%99%E7%AF%87%E5%B0%B1%E5%A4%9F%E4%BA%86/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="ES6（ECMAScript 2015）是JavaScript的一个重要更新版本。它引入了许多新的语言特性和改进，使JavaScript更加现代化和强大。以下是一些ES6的特性以及开发中的使用方法，话不多说开始ES6之旅吧~~~ let变量与const变量块级作用域在日常开发中多存在于使用if&#x2F;for关键字结合let&#x2F;const创建的块级作用域，值得注意的是使用let&amp;#x2F">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://dragon-xjy.atomgit.net/personal/image/es6/image5.png">
<meta property="og:image" content="https://dragon-xjy.atomgit.net/personal/image/es6/image2.png">
<meta property="og:image" content="https://dragon-xjy.atomgit.net/personal/image/es6/image3.png">
<meta property="og:image" content="https://dragon-xjy.atomgit.net/personal/image/es6/image4.png">
<meta property="article:published_time" content="2024-05-14T03:43:01.298Z">
<meta property="article:modified_time" content="2023-09-15T09:55:51.345Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="Es6">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://dragon-xjy.atomgit.net/personal/image/es6/image5.png">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="https://dragon-xjy.gitee.io/resource/blog/logo.jpg" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="https://dragon-xjy.gitee.io/resource/blog/logo.jpg">
    <meta name="theme-color" content="#A31F34">
    <link rel="shortcut icon" href="https://dragon-xjy.gitee.io/resource/blog/logo.jpg">
    <!--- Page Info-->
    
    <title>
        
            ES6这篇就够了！！ -
        
        XieJinYangの博客
    </title>
    
<link rel="stylesheet" href="/personal/css/style.css">


    
        
<link rel="stylesheet" href="/personal/css/build/styles.css">

    

    
<link rel="stylesheet" href="/personal/fonts/fonts.css">

    
<link rel="stylesheet" href="/personal/fonts/Satoshi/satoshi.css">

    
<link rel="stylesheet" href="/personal/fonts/Chillax/chillax.css">

    <!--- Font Part-->
    
    
    
        <link href="" rel="stylesheet">
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"dragon-xjy.atomgit.net","root":"/personal/","language":"zh-CN","path":"search.xml"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":false},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":["Lv Max"]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":true,"expand":true,"init_open":true},"copyright":true,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"mobile_limit":2,"placeholder":"/images/wallhaven-wqery6-light.webp","skip_dirs":["/photo/"]}},"colors":{"primary":"#A31F34","secondary":null},"global":{"fonts":{"chinese":{"enable":true,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":false,"percentage":true},"website_counter":{"url":"https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js","enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"single_page":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"fixed","image":{"light":"/images/light.jpg","dark":"/images/dark.png"},"title":"Personal blog","subtitle":{"text":["冰冻三尺非一日之寒","成大事者不拘小节"],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":true,"links":{"git":null,"fa-solid fa-user":"https://juejin.cn/user/1368213338982584","zhihu":null,"twitter":null,"email":"1821741004@qq.com"},"qrs":{"weixin":null}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":true,"type":"fixed","audios":[{"name":"7 Years","artist":"Lukas Graham","url":"https://dragon-xjy.gitee.io/resource/blog/c73b_cfbf_19df_d571e9501faef5e87a88caba137683ac.mp3","cover":"https://p1.music.126.net/-YNAznLwWX5jtyKexvAGEg==/109951164513163702.jpg"},{"name":"Beijing","artist":"接个吻，开一枪","url":"https://dragon-xjy.gitee.io/resource/blog/e8b8_eb80_50dc_72268dcc92f93e1bca64106756d652cb.mp3","cover":"https://p1.music.126.net/kXdnUtl-eVQcA15xDlSZWg==/109951163859434668.jpg?param=130y130"},{"name":"Something Just Like This","artist":"Coldplay","url":"https://evan.beee.top/music/Something%20Just%20Like%20This%20-%20The%20Chainsmokers%E3%80%81Coldplay.mp3","cover":"https://evan.beee.top/music/covers/Something_Just_Like_This.png"},{"name":"江山水意","artist":"九鸢Music","url":"https://dragon-xjy.gitee.io/resource/blog/%E6%B1%9F%E5%B1%B1%E6%B0%B4%E6%84%8F.mp3","cover":"https://p2.music.126.net/xJPohJAxnAViLo4IVk4Gmg==/109951163635180376.jpg?param=130y130"},{"name":"洪荒之力","artist":"Hoaprox","url":"https://dragon-xjy.gitee.io/resource/blog/705872029.mp3","cover":"https://p2.music.126.net/rtsE3wyy32vN3Q5NK4RncA==/109951168806948253.jpg"},{"name":"重生——《灵笼》","artist":"米浆mota","url":"https://dragon-xjy.gitee.io/resource/blog/obj_w5zDlMODwrDDiGjCn8Ky_2086216499_2338_e317_d2b4_8d00116ac5b5c8af88b46aa6840bd52f.mp3","cover":"https://p1.music.126.net/AZDJY7oNuxD_A910XCJeGw==/109951164349143568.jpg?param=130y130"}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.4.4","navbar":{"auto_hide":true,"color":{"left":"#f78736","right":"#367df7","transparency":10},"article":{"Article":{"icon":"fa-solid fa-folder","path":"/article/"}},"tags":{"Tags":{"icon":"fa-solid fa-tags","path":"/tags/"}},"categories":{"Categories":{"icon":"fa-solid fa-folder","path":"/categories/"}},"links":{"首页":{"path":"/","icon":"fa-regular fa-house"},"网页素材":{"path":"/photo","icon":"fa-solid fa-image"},"归档":{"icon":"fa-regular fa-archive","submenus":{"时间轴":"/archives","标签":"/tags","文章分类":"/categories"}},"友情链接":{"path":"/links/","icon":"fa-solid fa-link"}},"search":{"enable":true,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":null,"links":{"Archives":{"path":"/archives","icon":"fa-regular fa-archive"},"Tags":{"path":"/tags","icon":"fa-regular fa-tags"},"Categories":{"path":"/categories","icon":"fa-regular fa-folder"}}},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}},"footerStart":"2023/8/25 8:45:14"};
    Global.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    Global.data_config = {"masonry":true};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/personal/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/personal/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/personal/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/personal/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <span class="swup-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container" id="swup">

    

    <div class="main-content-container">


        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="https://dragon-xjy.atomgit.net/personal/">
                
                XieJinYangの博客
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/personal/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        首页
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/personal/photo"  >
                                    
                                        
                                            <i class="fa-solid fa-image"></i>
                                        
                                        网页素材
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="has-dropdown" 
                                    href="#" onClick="return false;">
                                    
                                        
                                            <i class="fa-regular fa-archive"></i>
                                        
                                        归档&nbsp;<i class="fa-solid fa-chevron-down"></i>
                                    
                                </a>
                                <!-- Submenu -->
                                
                                    <ul class="sub-menu">
                                    
                                        <li>
                                        <a href="/personal/archives">时间轴
                                        </a>
                                        </li>
                                    
                                        <li>
                                        <a href="/personal/tags">标签
                                        </a>
                                        </li>
                                    
                                        <li>
                                        <a href="/personal/categories">文章分类
                                        </a>
                                        </li>
                                    
                                    </ul>
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/personal/links/"  >
                                    
                                        
                                            <i class="fa-solid fa-link"></i>
                                        
                                        友情链接
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                    
                        <li class="navbar-item search search-popup-trigger">
                            <i class="fa-solid fa-magnifying-glass"></i>
                        </li>
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fa-solid fa-magnifying-glass"></i></div>
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer w-full absolute top-0 left-0 bg-background-color">
        <ul class="drawer-navbar-list flex flex-col justify-start items-center">
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group " 
                        href="/personal/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                首页
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group " 
                        href="/personal/photo"  >
                             
                                
                                    <i class="fa-solid fa-image"></i>
                                
                                网页素材
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group has-dropdown" 
                        href="#" onClick="return false;">
                            
                                
                                    <i class="fa-regular fa-archive"></i>
                                
                                归档&nbsp;<i class="group-hover:rotate-180 transition-transform fa-solid fa-chevron-down"></i>
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                              
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/personal/archives">时间轴</a>
                            </li>
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/personal/tags">标签</a>
                            </li>
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/personal/categories">文章分类</a>
                            </li>
                        
                    
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group " 
                        href="/personal/links/"  >
                             
                                
                                    <i class="fa-solid fa-link"></i>
                                
                                友情链接
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="post-page-container">
    <div class="article-content-container">

        <div class="article-title">
            
                
                
                <img src="/personal/images/es6/image.png" alt="ES6这篇就够了！！" class="max-w-none"/>
                
                <h1 class="article-title-cover">ES6这篇就够了！！</h1>
            
            </div>
            
                    
        
        
            <div class="article-header">
                <div class="avatar">
                    <img src="https://dragon-xjy.gitee.io/resource/blog/logo.jpg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">The局外人</span>
                        
                            <span class="author-label">Lv Max</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2024-05-14 11:43:01</span>
        <span class="mobile">2024-05-14 11:43:01</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-09-15 17:55:51</span>
            <span class="mobile">2023-09-15 17:55:51</span>
            <span class="hover-info">更新</span>
        </span>
    

    
        <span class="article-categories article-meta-item">
            <i class="fa-regular fa-folders"></i>&nbsp;
            <ul>
                
                
                    
                        
                        <li>
                            <a href="/personal/categories/JavaScript%E8%BF%9B%E9%98%B6/">JavaScript进阶</a>&nbsp;
                        </li>
                    
                    
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fa-regular fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/personal/tags/%E5%89%8D%E7%AB%AF/">前端</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/personal/tags/Es6/">Es6</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/personal/tags/JavaScript/">JavaScript</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        


        <div class="article-content markdown-body">
            <p>ES6（ECMAScript 2015）是JavaScript的一个重要更新版本。它引入了许多新的语言特性和改进，使JavaScript更加现代化和强大。以下是一些ES6的特性以及开发中的使用方法，话不多说开始ES6之旅吧~~~</p>
<h2 id="let变量与const变量"><a href="#let变量与const变量" class="headerlink" title="let变量与const变量"></a>let变量与const变量</h2><h3 id="块级作用域"><a href="#块级作用域" class="headerlink" title="块级作用域"></a>块级作用域</h3><p>在日常开发中多存在于使用<strong>if&#x2F;for</strong>关键字结合let&#x2F;const创建的块级作用域，值得注意的是使用let&#x2F;const关键字声明变量的for循环和var声明的<strong>有些不同</strong></p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> index=<span class="number">0</span>;index&lt;<span class="number">5</span>;index++)&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(index);<span class="comment">//index is not definded</span></span><br></pre></td></tr></table></figure></div>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> index=<span class="number">0</span>;index&lt;<span class="number">5</span>;index++)&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(index);<span class="comment">//5</span></span><br></pre></td></tr></table></figure></div>
<p>从而得出let声明的变量具有<strong>块级作用域</strong></p>
<h3 id="const"><a href="#const" class="headerlink" title="const"></a>const</h3><p>const常量声明一个只读属性的变量，<strong>不可更改</strong>，<strong>不可先声明后赋值</strong><br>举个栗子：</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj=&#123;<span class="attr">a</span>:<span class="number">1</span>&#125;;</span><br><span class="line">     obj.<span class="property">a</span>=<span class="number">2</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obj.<span class="property">a</span>);<span class="comment">//2</span></span><br></pre></td></tr></table></figure></div>
<p>这里不是说不能更改值吗？位啥改变a的值确不报错呢？这里就涉及js的一个<strong>堆与栈</strong>之间的关系了，对于引用数据类型，它是通过在<strong>栈</strong>中开辟一个<strong>地址</strong>去对相应堆中的<strong>值</strong>也就是<code>&#123;a:1&#125;</code>，而以上只是修改了堆中的值，而并没用引起栈中地址的变化。const声明的变量并没用变化。</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a=<span class="number">1</span>;</span><br><span class="line">      a=<span class="number">2</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a);<span class="comment">//Uncaught TypeError: Assignment to constant variable.</span></span><br></pre></td></tr></table></figure></div>
<p>这里对于基本数据类型，本生<strong>值</strong>就是存储在<strong>栈中</strong>，引起了值的变化。</p>
<h3 id="变量提升"><a href="#变量提升" class="headerlink" title="变量提升"></a>变量提升</h3><p>用let不存在声明提升，也叫暂时性死区ReferenceError</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a);<span class="comment">// ReferenceError:Cannot access &#x27;a&#x27; before initialization</span></span><br><span class="line">  <span class="keyword">let</span> a = <span class="number">1</span></span><br></pre></td></tr></table></figure></div>
<p>用var存在声明提升</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a);<span class="comment">// undefined</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">1</span></span><br></pre></td></tr></table></figure></div>
<h2 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h2><h3 id="基本用法"><a href="#基本用法" class="headerlink" title="基本用法"></a>基本用法</h3><div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title function_">fun</span>= v =&gt; v;</span><br><span class="line"><span class="comment">// 等同于</span></span><br><span class="line"><span class="keyword">let</span> f = <span class="keyword">function</span> (<span class="params">v</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> v;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></div>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="title function_">sum</span> = (<span class="params">num1, num2</span>) =&gt; &#123; <span class="keyword">return</span> num1 + num2; &#125;</span><br></pre></td></tr></table></figure></div>
<p>如果箭头函数的代码块部分多于一条语句，就要使用大括号将它们括起来，并且使用return语句返回。</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 报错</span></span><br><span class="line"><span class="keyword">let</span> <span class="title function_">fun</span> = id =&gt; &#123; <span class="attr">id</span>: id, <span class="attr">name</span>: <span class="string">&quot;Tom&quot;</span> &#125;;</span><br><span class="line"><span class="comment">// 不报错</span></span><br><span class="line"><span class="keyword">let</span> <span class="title function_">fun</span> = id =&gt; (&#123; <span class="attr">id</span>: id, <span class="attr">name</span>: <span class="string">&quot;Tom&quot;</span> &#125;);</span><br></pre></td></tr></table></figure></div>
<p>由于大括号被解释为代码块，所以如果箭头函数直接返回一个对象，必须在对象外面加上括号，否则会报错。</p>
<h3 id="this指向"><a href="#this指向" class="headerlink" title="this指向"></a>this指向</h3><div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">     <span class="attr">sayHi</span>: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">           <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;学习使我快乐&#x27;</span>);</span><br><span class="line">           <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;;</span><br><span class="line"> obj.<span class="title function_">sayHi</span>()<span class="comment">//this指向window</span></span><br></pre></td></tr></table></figure></div>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">  <span class="attr">sayHi</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;学习使我快乐&#x27;</span>);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line">obj.<span class="title function_">sayHi</span>()<span class="comment">//obj对象</span></span><br></pre></td></tr></table></figure></div>
<p>这里阐述了箭头函数中没有<strong>属于自己的this</strong>，会一层一层往外找，如不涉及this指向问题，建议使用箭头函数，减少代码量。</p>
<h2 id="解构赋值"><a href="#解构赋值" class="headerlink" title="解构赋值"></a>解构赋值</h2><h3 id="基本用法-1"><a href="#基本用法-1" class="headerlink" title="基本用法"></a>基本用法</h3><div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> arr= [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">const</span> [y, z] = arr;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(y); <span class="comment">// 1</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(z); <span class="comment">// 2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//对象解构</span></span><br><span class="line"><span class="keyword">const</span> person = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">  <span class="attr">attribute</span>: <span class="string">&#x27;法外狂徒&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> &#123;name,attribute&#125; = person;</span><br></pre></td></tr></table></figure></div>
<p>嵌套多层的情况</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr= [<span class="number">1</span>,[<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>],<span class="number">5</span>];</span><br><span class="line"><span class="keyword">let</span> [ a,[b,c,d],e ] = arr;</span><br><span class="line"><span class="comment">//a,b,c,d分别对应1，2，3，4，5</span></span><br></pre></td></tr></table></figure></div>
<h3 id="扩张运算符"><a href="#扩张运算符" class="headerlink" title="扩张运算符"></a>扩张运算符</h3><div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123; <span class="attr">a</span>: <span class="number">1</span>, <span class="attr">b</span>: <span class="number">2</span> &#125;;</span><br><span class="line"><span class="keyword">let</span> obj1= &#123; ...obj &#125;; <span class="comment">// &#123; a: 1, b: 2 &#125;</span></span><br></pre></td></tr></table></figure></div>
<p>同样，如果用户自定义的属性，放在扩展运算符后面，则扩展运算符内部的同名属性会被覆盖掉。</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;<span class="attr">a</span>: <span class="number">1</span>, <span class="attr">b</span>: <span class="number">2</span>&#125;;</span><br><span class="line"><span class="keyword">let</span> obj1 = &#123;...obj, ...&#123;<span class="attr">a</span>:<span class="number">2</span>, <span class="attr">b</span>: <span class="number">4</span>&#125;&#125;;  <span class="comment">// &#123;a: 2, b: 4&#125;</span></span><br></pre></td></tr></table></figure></div>
<p>这样，解构赋值与扩展运算符结合使用,可以极大解决提高开发效率</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> &#123; x, y, ...z &#125; = &#123; <span class="attr">x</span>: <span class="number">1</span>, <span class="attr">y</span>: <span class="number">2</span>, <span class="attr">a</span>: <span class="number">3</span>, <span class="attr">b</span>: <span class="number">4</span> &#125;;</span><br><span class="line">x <span class="comment">// 1</span></span><br><span class="line">y <span class="comment">// 2</span></span><br><span class="line">z <span class="comment">// &#123; a: 3, b: 4 &#125;</span></span><br></pre></td></tr></table></figure></div>



<h2 id="数组操作"><a href="#数组操作" class="headerlink" title="数组操作"></a>数组操作</h2><h3 id="数组合并"><a href="#数组合并" class="headerlink" title="数组合并"></a>数组合并</h3><div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> u1 = [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>];</span><br><span class="line"><span class="keyword">let</span> u2 = [<span class="string">&#x27;刘德华&#x27;</span>,<span class="string">&#x27;张学友&#x27;</span>,<span class="string">&#x27;郭富城&#x27;</span>];</span><br><span class="line"><span class="keyword">let</span> u3 = [...u1,...u2];</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(u3);<span class="comment">// [&#x27;张三&#x27;, &#x27;李四&#x27;, &#x27;王五&#x27;, &#x27;刘德华&#x27;, &#x27;张学友&#x27;, &#x27;郭富城&#x27;]</span></span><br></pre></td></tr></table></figure></div>
<h3 id="slice"><a href="#slice" class="headerlink" title="slice"></a>slice</h3><p>slice用于截取数组</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;刘德华&#x27;</span>, <span class="number">10</span>, <span class="string">&#x27;你好&#x27;</span>, <span class="string">&#x27;abc&#x27;</span>, <span class="number">20</span>,<span class="string">&#x27;张学友&#x27;</span>,<span class="string">&#x27;黎明&#x27;</span>]</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">slice</span>(<span class="number">1</span>,<span class="number">5</span>));<span class="comment">//[10, &#x27;你好&#x27;, &#x27;abc&#x27;, 20]</span></span><br></pre></td></tr></table></figure></div>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="/personal/image/es6/image5.png"
                      alt="image1.png"
                ><br>代码看出，<code>slice(1,5)</code>中1,5代表数组的索引，且<strong>索引包前不包后，</strong>只包括索引为1，不包括索引为5，且最终返回值为<strong>满足条件的数组</strong></p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="string">&#x27;刘德华&#x27;</span>, <span class="number">10</span>, <span class="string">&#x27;你好&#x27;</span>, <span class="string">&#x27;abc&#x27;</span>, <span class="number">20</span>,<span class="string">&#x27;张学友&#x27;</span>,<span class="string">&#x27;黎明&#x27;</span>]</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(arr.<span class="title function_">slice</span>(<span class="number">1</span>));<span class="comment">//[10, &#x27;你好&#x27;, &#x27;abc&#x27;, 20, &#x27;张学友&#x27;, &#x27;黎明&#x27;]</span></span><br></pre></td></tr></table></figure></div>
<p>当参数只有一个时，这个参数就代表开始开始那个，上述代码代表以索引为1的那个元素开始，截取后面所有的元素。</p>
<h3 id="indexOf"><a href="#indexOf" class="headerlink" title="indexOf"></a>indexOf</h3><p>indexOf可用于查找数组</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]</span><br><span class="line">       <span class="keyword">let</span> a = arr.<span class="title function_">indexOf</span>(<span class="number">8</span>)</span><br><span class="line">       <span class="keyword">let</span> b=arr.<span class="title function_">indexOf</span>(<span class="number">9</span>)</span><br><span class="line">       <span class="variable language_">console</span>.<span class="title function_">log</span>(a);<span class="comment">//7</span></span><br><span class="line">       <span class="variable language_">console</span>.<span class="title function_">log</span>(b);<span class="comment">//-1</span></span><br></pre></td></tr></table></figure></div>
<p>indexOf中第一个参数代表你要<strong>查询的元素</strong>，如上述代码查找元素8，查到就返回元素的<strong>索引</strong>，<br>如果没查到就返回-1。</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]</span><br><span class="line">       <span class="keyword">let</span> a = arr.<span class="title function_">indexOf</span>(<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line">       <span class="variable language_">console</span>.<span class="title function_">log</span>(a);<span class="comment">//-1</span></span><br></pre></td></tr></table></figure></div>
<p>对于indexOf中第二个参数表示传入<strong>数组的索引</strong>，上述代码代表：从索引为3的地方查找元素2。</p>
<h3 id="includes"><a href="#includes" class="headerlink" title="includes"></a>includes</h3><div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> users = [<span class="string">&#x27;张三&#x27;</span>,<span class="string">&#x27;李四&#x27;</span>];</span><br><span class="line"><span class="keyword">let</span> res =users.<span class="title function_">includes</span>(<span class="string">&#x27;张三&#x27;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(res);<span class="comment">//true</span></span><br></pre></td></tr></table></figure></div>
<p>注：includes判断依据是&#x3D;&#x3D;&#x3D;（全等于）</p>
<h3 id="find"><a href="#find" class="headerlink" title="find"></a>find</h3><p>find 用来找到<strong>第一个</strong>符合条件的成员，没有找到返回 <strong>undefined，</strong>结果返回<strong>对象</strong></p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> user = [&#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;];</span><br><span class="line"><span class="keyword">let</span> newUser = user.<span class="title function_">find</span>(<span class="function">(<span class="params">item, index</span>) =&gt;</span></span><br><span class="line">  item.<span class="property">age</span> &gt; <span class="number">18</span></span><br><span class="line">)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(newUser);<span class="comment">//&#123;name: &#x27;李四&#x27;, age: 20&#125;</span></span><br></pre></td></tr></table></figure></div>
<h3 id="findIndex"><a href="#findIndex" class="headerlink" title="findIndex"></a>findIndex</h3><p>findIndex 用来找到<strong>第一个</strong>符合条件的成员的索引，没有的话返回-1</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> user= [&#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;];</span><br><span class="line"><span class="keyword">let</span> index = user.<span class="title function_">findIndex</span>(<span class="function">(<span class="params">item, index</span>) =&gt;</span></span><br><span class="line">    item.<span class="property">age</span> &gt; <span class="number">18</span></span><br><span class="line">)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(index)<span class="comment">//1</span></span><br></pre></td></tr></table></figure></div>
<h3 id="filter"><a href="#filter" class="headerlink" title="filter"></a>filter</h3><div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> user = [&#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;];</span><br><span class="line"><span class="keyword">let</span> array = user.<span class="title function_">filter</span>(<span class="function">(<span class="params">item, index</span>) =&gt;</span></span><br><span class="line">    item.<span class="property">age</span> &gt; <span class="number">19</span></span><br><span class="line">)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(array);</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<p>结果返回数组<br><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="/personal/image/es6/image2.png"
                      alt="image.png"
                ><br>妙用filter</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [&#123;<span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>,<span class="attr">age</span>: <span class="number">20</span>&#125;,&#123;<span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">25</span> &#125;]</span><br><span class="line">       arr = arr.<span class="title function_">filter</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">           <span class="keyword">if</span> (item.<span class="property">age</span> &gt;<span class="number">20</span>) &#123;</span><br><span class="line">               item.<span class="property">name</span> = <span class="string">&#x27;王五&#x27;</span></span><br><span class="line">           &#125;</span><br><span class="line">           <span class="comment">//妙用</span></span><br><span class="line">           <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">       &#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<p>filter不仅能过滤数组对象，还能<strong>修改</strong>数组对象的<strong>属性值</strong>，return true妙用</p>
<h3 id="map"><a href="#map" class="headerlink" title="map"></a>map</h3><p>map 用来返回一个对成员进行加工之后的新数组</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> users = [&#123; <span class="attr">name</span>: <span class="string">&#x27;张三&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;李四&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;];</span><br><span class="line"><span class="keyword">let</span> array = users.<span class="title function_">map</span>(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">name1</span>: item.<span class="property">name</span>,</span><br><span class="line">    <span class="attr">age1</span>: <span class="number">30</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(array);</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="/personal/image/es6/image3.png"
                      alt="image.png"
                ></p>
<h3 id="every与some"><a href="#every与some" class="headerlink" title="every与some"></a>every与some</h3><p>注：every方法return一个条件，必须<strong>全部满足此条件返回true</strong>，有一条不满足，就返回false</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [&#123; <span class="attr">name</span>: <span class="string">&#x27;刘德华&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;张学友&#x27;</span>, <span class="attr">age</span>: <span class="number">25</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;郭富城&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span> &#125;]</span><br><span class="line"><span class="keyword">const</span> result = arr.<span class="title function_">every</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> item.<span class="property">age</span> &gt; <span class="number">26</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result);<span class="comment">//false</span></span><br></pre></td></tr></table></figure></div>
<p>注：some方return一个条件，只要有<strong>一个</strong>满足条件，就返回true</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [&#123; <span class="attr">name</span>: <span class="string">&#x27;刘德华&#x27;</span>, <span class="attr">age</span>: <span class="number">20</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;张学友&#x27;</span>, <span class="attr">age</span>: <span class="number">25</span> &#125;, &#123; <span class="attr">name</span>: <span class="string">&#x27;郭富城&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span> &#125;]</span><br><span class="line"><span class="keyword">const</span> result = arr.<span class="title function_">some</span>(<span class="function">(<span class="params">item</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> item.<span class="property">age</span> &gt; <span class="number">26</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result);<span class="comment">//true</span></span><br></pre></td></tr></table></figure></div>
<p>总结：every与some的区别是：every（一假及假），some（一真及真）</p>
<h3 id="reduce"><a href="#reduce" class="headerlink" title="reduce"></a>reduce</h3><p>使用场景<strong>数组累加</strong>和<strong>操作数据组成新的数组</strong></p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line"><span class="keyword">const</span> nums = arr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">num, item</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> num += item</span><br><span class="line">&#125;, <span class="number">0</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(nums);<span class="comment">//55</span></span><br></pre></td></tr></table></figure></div>
<p>这里提及一些不同的用法：</p>
<div class="highlight-container" data-rel="Javascript"><figure class="iseeu highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]</span><br><span class="line"><span class="keyword">const</span> nums = arr.<span class="title function_">reduce</span>(<span class="function">(<span class="params">arr, item</span>) =&gt;</span> &#123;</span><br><span class="line">  arr.<span class="title function_">push</span>(&#123; <span class="attr">oldNum</span>: item, <span class="attr">newNum</span>: item + <span class="number">1</span> &#125;)</span><br><span class="line">  <span class="keyword">return</span> arr</span><br><span class="line">&#125;, [])</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(newArr);</span><br></pre></td></tr></table></figure></div>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="/personal/image/es6/image4.png"
                      alt="Snipaste_2023-09-02_20-42-56.png"
                ><br>当你需要操作<strong>数据组成新的数组或对象</strong>时，可以用这种方式，实际开发中得看具体逻辑.</p>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>很多的方法我只讲最基础的，具体在开发中怎样去灵活运用，需要自己摸索，这里只是总结了一小部分Es6相关知识，后面我将不断完善，此外以上的整理内容有误的地方，欢迎在评论中指正，万分感谢。</p>

        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> ES6这篇就够了！！</li>
        <li><strong>作者:</strong> The局外人</li>
        <li><strong>创建于
                :</strong> 2024-05-14 11:43:01</li>
        
            <li>
                <strong>更新于
                    :</strong> 2023-09-15 17:55:51
            </li>
        
        <li>
            <strong>链接:</strong> https://dragon-xjy.gitee.io/2024/05/14/关于Es6这篇就够了/
        </li>
        <li>
            <strong>
                版权声明:
            </strong>
            
            本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0</a> 进行许可。
            

        </li>
    </ul>
</div>

            </div>
        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/personal/tags/%E5%89%8D%E7%AB%AF/">#前端</a>&nbsp;
                    </li>
                
                    <li class="tag-item">
                        <a href="/personal/tags/Es6/">#Es6</a>&nbsp;
                    </li>
                
                    <li class="tag-item">
                        <a href="/personal/tags/JavaScript/">#JavaScript</a>&nbsp;
                    </li>
                
            </ul>
        

        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                        rel="prev"
                        href="/personal/2024/05/14/%E9%80%9A%E4%BF%97%E6%98%93%E6%87%82%E4%B9%8BPromise/"
                        >
                            <span class="left arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-left"></i>
                            </span>
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">通俗易懂之Promise!</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                        rel="next"
                        href="/personal/2024/05/14/%E5%85%AC%E4%BC%97%E5%8F%B7%E5%AE%9E%E7%8E%B0%E5%BE%AE%E4%BF%A1%E7%BD%91%E9%A1%B5%E6%8E%88%E6%9D%83/"
                        >
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">公众号实现微信网页授权</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        


        
            <div class="comment-container">
                <div class="comments-container pjax">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fa-solid fa-comments"></i>&nbsp;评论
    </div>
    

        
            
 
    <div id="waline"></div>
    <script type="module"  data-swup-reload-script>
        import { init } from 'https://evan.beee.top/js/waline.mjs';

        function loadWaline() {
            init({
                el: '#waline',
                serverURL: 'https://waline-xjy-blog.oknice.ca/',
                lang: 'zh-CN',
                dark: 'body[class~="dark-mode"]',
                requiredMeta: ['nick','mail'], // cannot customize by theme config, change it yourself
            });
        }

        if ('true') {
            const loadWalineTimeout = setTimeout(() => {
                loadWaline();
                clearTimeout(loadWalineTimeout);
            }, 1000);
        } else {
            window.addEventListener('DOMContentLoaded', loadWaline);
        }
        
    </script>



        
    
</div>

            </div>
        
    </div>

    
        <div class="toc-content-container">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">ES6这篇就够了！！</div>
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#let%E5%8F%98%E9%87%8F%E4%B8%8Econst%E5%8F%98%E9%87%8F"><span class="nav-number">1.</span> <span class="nav-text">let变量与const变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9D%97%E7%BA%A7%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number">1.1.</span> <span class="nav-text">块级作用域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#const"><span class="nav-number">1.2.</span> <span class="nav-text">const</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%98%E9%87%8F%E6%8F%90%E5%8D%87"><span class="nav-number">1.3.</span> <span class="nav-text">变量提升</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="nav-number">2.</span> <span class="nav-text">箭头函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95"><span class="nav-number">2.1.</span> <span class="nav-text">基本用法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#this%E6%8C%87%E5%90%91"><span class="nav-number">2.2.</span> <span class="nav-text">this指向</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A7%A3%E6%9E%84%E8%B5%8B%E5%80%BC"><span class="nav-number">3.</span> <span class="nav-text">解构赋值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95-1"><span class="nav-number">3.1.</span> <span class="nav-text">基本用法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%A9%E5%BC%A0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">3.2.</span> <span class="nav-text">扩张运算符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E6%93%8D%E4%BD%9C"><span class="nav-number">4.</span> <span class="nav-text">数组操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E5%90%88%E5%B9%B6"><span class="nav-number">4.1.</span> <span class="nav-text">数组合并</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#slice"><span class="nav-number">4.2.</span> <span class="nav-text">slice</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#indexOf"><span class="nav-number">4.3.</span> <span class="nav-text">indexOf</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#includes"><span class="nav-number">4.4.</span> <span class="nav-text">includes</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#find"><span class="nav-number">4.5.</span> <span class="nav-text">find</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#findIndex"><span class="nav-number">4.6.</span> <span class="nav-text">findIndex</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#filter"><span class="nav-number">4.7.</span> <span class="nav-text">filter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#map"><span class="nav-number">4.8.</span> <span class="nav-text">map</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#every%E4%B8%8Esome"><span class="nav-number">4.9.</span> <span class="nav-text">every与some</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#reduce"><span class="nav-number">4.10.</span> <span class="nav-text">reduce</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E8%AF%AD"><span class="nav-number">5.</span> <span class="nav-text">结语</span></a></li></ol>

    </div>
</div>
        </div>
    
</div>



                

            </div>

            

        </div>

        <div class="main-content-footer">
            <footer class="footer mt-5 py-5 h-auto text-base text-third-text-color relative border-t-2 border-t-border-color">
    <div class="info-container py-3 text-center">
        
        <div class="text-center">
            &copy;
            
              <span>2023</span>
              -
            
            2024&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">The局外人</a>
        </div>
        
            <script data-swup-reload-script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="relative text-center lg:absolute lg:right-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-right">
                
                    <span id="busuanzi_container_site_uv" class="lg:!block">
                        <span class="text-sm">访问人数</span>
                        <span id="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="lg:!block">
                        <span class="text-sm">总访问量</span>
                        <span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="relative text-center lg:absolute lg:left-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-left">
            <span class="lg:block text-sm">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg class="relative top-[2px] inline-block align-baseline" version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" class="text-base" href="https://hexo.io">Hexo</a> 驱动</span>
            <span class="text-sm lg:block">主题&nbsp;<a class="text-base" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.4.4</a></span>
        </div>
        
        
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
            <script data-swup-reload-script>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fa-regular fa-comments"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex justify-center items-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex justify-center items-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex justify-center items-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex justify-center items-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex justify-center items-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fa-solid fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fa-solid fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa-solid fa-spinner fa-spin-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>


    
<script src="/personal/js/libs/Swup.min.js"></script>

<script src="/personal/js/libs/SwupSlideTheme.min.js"></script>

<script src="/personal/js/libs/SwupScriptsPlugin.min.js"></script>

<script src="/personal/js/libs/SwupProgressPlugin.min.js"></script>

<script src="/personal/js/libs/SwupScrollPlugin.min.js"></script>

<script>
    const swup = new Swup({
        plugins: [
            new SwupScriptsPlugin({
                optin: true,
            }),
            new SwupProgressPlugin(),
            new SwupScrollPlugin({
                offset: 80,
            }),
            new SwupSlideTheme({
                mainElement: ".main-content-body",
            }),
        ],
        containers: ["#swup"],
    });

    swup.hooks.on("page:view", () => {
        Global.refresh();
    });

    // if (document.readyState === "complete") {
    //
    // } else {
    //     document.addEventListener("DOMContentLoaded", () => init());
    // }
</script>






<script src="/personal/js/utils.js" type="module"></script>

<script src="/personal/js/main.js" type="module"></script>

<script src="/personal/js/layouts/navbarShrink.js" type="module"></script>

<script src="/personal/js/tools/scrollTopBottom.js" type="module"></script>

<script src="/personal/js/tools/lightDarkSwitch.js" type="module"></script>

<script src="/personal/js/layouts/categoryList.js" type="module"></script>



    
<script src="/personal/js/tools/localSearch.js"></script>




    
<script src="/personal/js/tools/codeBlock.js"></script>




    
<script src="/personal/js/layouts/lazyload.js"></script>




    
<script src="/personal/js/tools/runtime.js"></script>

    
<script src="/personal/js/libs/odometer.min.js"></script>

    
<link rel="stylesheet" href="/personal/assets/odometer-theme-minimal.css">




  
<script src="/personal/js/libs/Typed.min.js"></script>

  
<script src="/personal/js/plugins/typed.js"></script>






    
<script src="/personal/js/libs/minimasonry.min.js"></script>

    
<script src="/personal/js/plugins/masonry.js" type="module"></script>




<div class="post-scripts" data-swup-reload-script>
    
        
<script src="/personal/js/libs/anime.min.js"></script>

        
<script src="/personal/js/tools/tocToggle.js" type="module"></script>

<script src="/personal/js/layouts/toc.js" type="module"></script>

<script src="/personal/js/plugins/tabs.js" type="module"></script>

    
</div>


    <div id="aplayer"></div>

<script src="/personal/js/libs/APlayer.min.js"></script>


<script src="/personal/js/plugins/aplayer.js"></script>


</body>
</html>
